const { DataTypes } = require('sequelize')
const { sequelize } = require('../config/database')

const Manga = sequelize.define('Manga', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false
  },
  author: {
    type: DataTypes.STRING,
    allowNull: true
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  cover: {
    type: DataTypes.STRING,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('ongoing', 'completed', 'hiatus', 'cancelled'),
    defaultValue: 'ongoing'
  },
  source: {
    type: DataTypes.STRING,
    allowNull: false
  },
  sourceId: {
    type: DataTypes.STRING,
    allowNull: false
  },
  sourceUrl: {
    type: DataTypes.STRING,
    allowNull: false
  },
  categories: {
    type: DataTypes.JSON,
    defaultValue: []
  },
  tags: {
    type: DataTypes.JSON,
    defaultValue: []
  },
  rating: {
    type: DataTypes.FLOAT,
    defaultValue: 0
  },
  views: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  lastUpdate: {
    type: DataTypes.DATE,
    allowNull: true
  },
  totalChapters: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  indexes: [
    {
      fields: ['title']
    },
    {
      fields: ['author']
    },
    {
      fields: ['source', 'sourceId']
    }
  ]
})

module.exports = Manga
